package person;

import gui.MainGUI;

import java.util.Vector;

/**
 * Singleton
 * 
 * @author Minodor Keeps track of all persons/actors in the scenario
 */
public class PersonAchive {
	private static final PersonAchive instance = new PersonAchive();

	public static int getPersonIdentifier() {
		String id = instance.toString();
		int pid = id.hashCode();
		pid = Math.abs(pid);
		return pid;
	}

	public static boolean add(Person newPerson) {
		Person p = getPerson(newPerson.getName());
		if (p != null)
			return false;
		else {
			instance.archive.add(newPerson);
			MainGUI.familyVal = true;
			MainGUI.setVal();
			return true;
		}

	}

	public static int getCount() {
		return instance.archive.size();
	}

	public static Vector<Person> getList() {
		return PersonAchive.instance.archive;

	}

	public static Person getPerson(int selectedIndex) {
		return PersonAchive.instance.archive.get(selectedIndex);

	}

	public static Person getPerson(String name) {
		for (int index = 0; index < instance.archive.size(); index++) {
			if (instance.archive.get(index).getName().equalsIgnoreCase(name))
				return instance.archive.get(index);
		}
		return null;
	}

	public static void remove(String firstName, String lastName) {
		for (int i = 0; i < instance.archive.size(); i++) {
			if (instance.archive.get(i).getName().equalsIgnoreCase(firstName)
					&& instance.archive.get(i).getLastName().equalsIgnoreCase(
							lastName)) {
				instance.archive.remove(i);
				break;
			}
		}
	}

	public static void reset() {
		MainGUI.familyVal = false;
		MainGUI.setVal();
		instance.archive = new Vector<Person>();

	}

	public static void setHome(int i, Location l) {
		instance.archive.get(i).setCurrentLocation(l);
	}

	public static void update(int i, Person p) {
		instance.archive.set(i, p);
	}

	private Vector<Person> archive;

	private PersonAchive() {
		archive = new Vector<Person>();

	}

	public String toString() {
		String res = new String();
		for (int i = 0; i < getCount(); i++)
			res += "\n" + PersonAchive.getPerson(i).print();
		return res;
	}

}
